package com.next

import grails.converters.JSON
import org.springframework.security.access.annotation.Secured

import java.text.SimpleDateFormat

import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_COMPANY_USER'])
class SalesDashboardController
{
    def contactService
    def opportunityService
    def springSecurityService

    def index()
    {
    }

    def cashierDashboard()
    {
        def username = springSecurityService?.getPrincipal()?.username
        if (!UserRole.findByUserAndRole(User.findByUsername(username), Role.findByAuthority('ROLE_ADMINISTRATOR')) && !UserRole.findByUserAndRole(User.findByUsername(username), Role.findByAuthority('ROLE_COMPANY_CASHIER')))
        {
            return
        }
        def code = params['code']
        def accountName = params['accountName']
        def accountSql = "from Account where 1=1 "
        def leadSql = "from Lead where active = true "
        def activitySql = "from Activity where 1=1 "
        def opportunitySql = "from Opportunity where 1=1 "
        if (code)
        {
            accountSql += "and id = ${code} "
            leadSql += "and id = ${code} "
            activitySql += "and id = ${code} "
            opportunitySql += "and id = ${code} "
        }
        if (accountName)
        {
            accountSql += "and name like '%${accountName}%' "
            leadSql += "and account.name like '%${accountName}%' "
            activitySql += "and account.name like '%${accountName}%' "
            opportunitySql += "and account.name like '%${accountName}%' "
        }
        def accountList = (code || accountName) ? Account.findAll(accountSql) : []
        def leadList = (code || accountName) ? Lead.findAll(leadSql) : []
        def activityList = (code || accountName) ? Activity.findAll(activitySql) : []
        def opportunityList = (code || accountName) ? Opportunity.findAll(opportunitySql) : []

        respond([accountList: accountList, leadList: leadList, activityList: activityList, opportunityList: opportunityList, params: params])
    }

    def loadIndexData()
    {
        def username = springSecurityService?.getPrincipal()?.username
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM")
        Calendar currentDay = Calendar.getInstance()
        Calendar currentWeek = Calendar.getInstance()
        Calendar currentMonth = Calendar.getInstance()
        Calendar currentSeason = Calendar.getInstance()
        Calendar currentYear = Calendar.getInstance()
        Calendar endWeek = Calendar.getInstance()
        Calendar endMonth = Calendar.getInstance()
        Calendar endSeason = Calendar.getInstance()
        Calendar endYear = Calendar.getInstance()
        int season = Math.floor((currentSeason.get(Calendar.MONTH)) / 3)
        if (currentWeek.get(Calendar.DAY_OF_WEEK) == 1)
        {
            currentWeek.add(Calendar.DATE, -6)
        }
        else
        {
            currentWeek.set(Calendar.DAY_OF_WEEK, 2)
        }

        currentMonth.set(Calendar.DAY_OF_MONTH, 1)
        currentSeason.set(Calendar.DAY_OF_MONTH, 1)
        currentSeason.set(Calendar.MONTH, 3 * season)
        currentYear.set(Calendar.DAY_OF_YEAR, 1)
        endWeek.set(Calendar.DAY_OF_WEEK, 1)
        endWeek.add(Calendar.DATE, 7)
        endMonth.set(Calendar.DAY_OF_MONTH, 1)
        endMonth.add(Calendar.MONTH, 1)
        endSeason.set(Calendar.DAY_OF_MONTH, 1)
        if (season == 4)
        {
            endSeason.set(Calendar.MONTH, 0)
            endSeason.add(Calendar.YEAR, 1)
        }
        else
        {
            endSeason.set(Calendar.MONTH, 3 * season)
        }
        endYear.set(Calendar.DAY_OF_YEAR, 1)
        endYear.add(Calendar.YEAR, 1)
        String cDay = sdf.format(currentDay.getTime())
        String cWeek = sdf.format(currentWeek.getTime())
        String cMonth = sdf.format(currentMonth.getTime())
        String cSeason = sdf.format(currentSeason.getTime())
        String cYear = sdf.format(currentYear.getTime())
        String eWeek = sdf.format(endWeek.getTime())
        String eMonth = sdf.format(endMonth.getTime())
        String eSeason = sdf.format(endSeason.getTime())
        String eYear = sdf.format(endYear.getTime())
        def amount = []
        def amount2 = []
        def amount3 = []
        def amount4 = []
        def amount5 = []
        for (
            def i = 0;
                i < 20;
                i++)
        {
            amount[i] = 0
            amount2[i] = 0
            amount3[i] = 0
            amount4[i] = 0
            amount5[i] = 0
        }

        // def opportunityList = Opportunity.findAll("from Opportunity where createdDate >= '${cMonth}' and user.username = '${username}'")

        // def activityList = Activity.findAll("from Activity where startTime>='${cYear}' and user.username = '${username}'")

        // def opportunityList = Opportunity.findAll("from Opportunity where createdDate >= to_date('${cMonth}','YYYY-MM-DD HH24:MI:SS') and user.username = '${username}'")
        def opportunityList = Opportunity.findAll("from Opportunity where createdDate >= to_date('${cYear}','YYYY-MM-DD HH24:MI:SS') and user.username = '${username}'")

        def activityList = Activity.findAll("from Activity where startTime >= to_date('${cYear}','YYYY-MM-DD HH24:MI:SS') and user.username = '${username}'")
        def contractList = Contract.findAll("from Contract where createdDate >= to_date('${cYear}','YYYY-MM-DD HH24:MI:SS') and user.username = '${username}'")

        activityList.each {
            if (it.type.name == 'Appointment')
            {
                if (it.startTime.after(currentDay.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[0]++
                    }
                    else
                    {
                        amount[1]++
                    }
                }
                if (it.startTime.after(currentWeek.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[2]++
                    }
                    else
                    {
                        amount[3]++
                    }
                }
                if (it.startTime.after(currentMonth.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[4]++
                    }
                    else
                    {
                        amount[5]++
                    }
                }
                if (it.startTime.after(currentSeason.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[6]++
                    }
                    else
                    {
                        amount[7]++
                    }
                }
                if (it.startTime.after(currentYear.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[8]++
                    }
                    else
                    {
                        amount[9]++
                    }
                }
            }
            if (it.type.name == 'Task')
            {
                if (it.startTime.after(currentDay.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[10]++
                    }
                    else
                    {
                        amount[11]++
                    }
                }
                if (it.startTime.after(currentWeek.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[12]++
                    }
                    else
                    {
                        amount[13]++
                    }
                }
                if (it.startTime.after(currentMonth.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[14]++
                    }
                    else
                    {
                        amount[15]++
                    }
                }
                if (it.startTime.after(currentSeason.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[16]++
                    }
                    else
                    {
                        amount[17]++
                    }
                }
                if (it.startTime.after(currentYear.getTime()))
                {
                    if (it.status == '已完成')
                    {
                        amount[18]++
                    }
                    else
                    {
                        amount[19]++
                    }
                }
            }
        }

        opportunityList.each {
            if (it.createdDate.after(currentWeek.getTime()))
            {
                if (it.stage?.name == '已入围')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount2[0] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount2[1] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount2[2] += it.amount
                    }
                }
                if (it.stage?.name == '需求分析')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount2[3] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount2[4] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount2[5] += it.amount
                    }
                }
                if (it.stage?.name == '解决方案')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount2[6] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount2[7] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount2[8] += it.amount
                    }
                }
                if (it.stage?.name == '谈判')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount2[9] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount2[10] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount2[11] += it.amount
                    }
                }
                if (it.stage?.name == '接近成功')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount2[12] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount2[13] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount2[14] += it.amount
                    }
                }
            }
            if (it.createdDate.after(currentMonth.getTime()))
            {
                if (it.stage?.name == '已入围')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount3[0] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount3[1] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount3[2] += it.amount
                    }
                }
                if (it.stage?.name == '需求分析')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount3[3] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount3[4] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount3[5] += it.amount
                    }
                }
                if (it.stage?.name == '解决方案')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount3[6] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount3[7] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount3[8] += it.amount
                    }
                }
                if (it.stage?.name == '谈判')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount3[9] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount3[10] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount3[11] += it.amount
                    }
                }
                if (it.stage?.name == '接近成功')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount3[12] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount3[13] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount3[14] += it.amount
                    }
                }
            }
            if (it.createdDate.after(currentSeason.getTime()))
            {
                if (it.stage?.name == '已入围')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount4[0] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount4[1] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount4[2] += it.amount
                    }
                }
                if (it.stage?.name == '需求分析')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount4[3] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount4[4] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount4[5] += it.amount
                    }
                }
                if (it.stage?.name == '解决方案')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount4[6] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount4[7] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount4[8] += it.amount
                    }
                }
                if (it.stage?.name == '谈判')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount4[9] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount4[10] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount4[11] += it.amount
                    }
                }
                if (it.stage?.name == '接近成功')
                {
                    if (it.status?.name == '跟进中')
                    {
                        amount4[12] += it.amount
                    }
                    if (it.status?.name == '已成功')
                    {
                        amount4[13] += it.amount
                    }
                    if (it.status?.name == '已失败')
                    {
                        amount4[14] += it.amount
                    }
                }
            }

        }

        contractList.each {
            if (it.createdDate.after(currentWeek.getTime()))
            {
                if (it.status?.name == '已签订合同')
                {
                    amount5[0] += it?.amount ?: 0
                }
                if (it.status?.name == '已发货')
                {
                    amount5[1] += it?.amount ?: 0
                }
                if (it.status?.name == '已验收')
                {
                    amount5[2] += it?.amount ?: 0
                }
                if (it.status?.name == '已付款')
                {
                    amount5[3] += it?.amount ?: 0
                }
                if (it.status?.name == '结束')
                {
                    amount5[4] += it?.amount ?: 0
                }
            }
            if (it.createdDate.after(currentMonth.getTime()))
            {
                if (it.status?.name == '已签订合同')
                {
                    amount5[5] += it?.amount ?: 0
                }
                if (it.status?.name == '已发货')
                {
                    amount5[6] += it?.amount ?: 0
                }
                if (it.status?.name == '已验收')
                {
                    amount5[7] += it?.amount ?: 0
                }
                if (it.status?.name == '已付款')
                {
                    amount5[8] += it?.amount ?: 0
                }
                if (it.status?.name == '结束')
                {
                    amount5[9] += it?.amount ?: 0
                }
            }
            if (it.createdDate.after(currentSeason.getTime()))
            {
                if (it.status?.name == '已签订合同')
                {
                    amount5[10] += it?.amount ?: 0
                }
                if (it.status?.name == '已发货')
                {
                    amount5[11] += it?.amount ?: 0
                }
                if (it.status?.name == '已验收')
                {
                    amount5[12] += it?.amount ?: 0
                }
                if (it.status?.name == '已付款')
                {
                    amount5[13] += it?.amount ?: 0
                }
                if (it.status?.name == '结束')
                {
                    amount5[14] += it?.amount ?: 0
                }
            }
        }
        render([amount: amount, amount2: amount2, amount3: amount3, amount4: amount4, amount5: amount5] as JSON)
    }
}
